package com.example.mq.mqserver.datacenter;


import com.example.mq.DemoApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/*
* 通过这个类整合数据库操作*/
public class DatabaseManager {
    // 单例对象
    private static final DatabaseManager instance = new DatabaseManager();

    //外部方法获取单例
    public static DatabaseManager getInstance() {
        return instance;
    }

    private MetaMapper metaMapper;

    //删除数据库
    public void deleteDB(){
        File file=new File("./data/meta.db");// .代表当前工作目录
        boolean ret=file.delete();
        if(ret){
            System.out.println("【DatabaseManager】 数据库删除成功");
        }else {
            System.out.println("【DatabaseManager】 数据库删除失败");

        }
    }

    //针对数据库进行初始化
    public void init(){
        if(!checkDBExists()){

            //创建data文件夹在当前项目路劲
            File dataDir=new File("./data");
            dataDir.mkdirs();

            //手动获取metaMapper
            metaMapper= DemoApplication.context.getBean(MetaMapper.class);
            System.out.println();
            //不存在，则建表
            createTable();

            //插入默认数据
            createDefaultData();
            System.out.println("【DatabaseManager】数据库初始化完成！");
        }else{
            System.out.println("【DatabaseManager】数据库已经存在!");
        }
    }

    //创建默认交换机 类型是Direct
    private void createDefaultData() {
        //构造一个默认交换机
        Exchange exchange=new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("【DatabaseManager】创建初始数据完成");

    }

    //这个方法用来建表
    //数据库不用创建，在第一次运行是它自动创建meta.db文件夹（mybatis帮我们完成）
    private void createTable() {
            metaMapper.createExchangeTable();
            metaMapper.createQueueTable();
            metaMapper.createBindingTable();
        System.out.println("【DatabaseManager】 创建三个表完成");
    }

    private boolean checkDBExists() {
        File file=new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }


    public List<Exchange> selectAllExchange(){
        return metaMapper.selectAllExchanges();
    }
    public List<MSGQueue> selectAllQueue(){
        return metaMapper.selectAllQueues();
    }
    public List<Binding> selectAllBinding(){
        return metaMapper.selectAllBindings();
    }

    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(Exchange exchange){
        metaMapper.deleteExchange(exchange.getName());
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public void deleteQueue(MSGQueue queue){
        metaMapper.deleteQueue(queue.getName());
    }
    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
